package com.silences.service;

import com.silences.dao.BaseDao;
import com.silences.pojo.Doods;
import com.silences.utils.JDBCUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//商品表 业务操作
public class DoodsService {
    BaseDao baseDao =new BaseDao();//创建BaseDao对象
    // 列表查询
    public Map<String,Object> getDoodss(String name, int pageIndex, int pageSize){
        Map<String,Object> map =new HashMap<String,Object>();
        try {
            //1 编写sql语句
            String sql="select * from doods where 1=1"; //1=1恒等于true ，拼接where关键词
            String  countSql="select count(*) from doods where 1=1";
            //2 判断是否有搜索关键词，有则拼接
            List<Object> list = new ArrayList<Object>();//
            if(name !=null && name !=""){
                sql +=" and name like concat('%', ?, '%') ";//name like concat('%', ?, '%') "; 这个叫模糊查询
                countSql +=" and name like concat('%', ?, '%') ";
                list.add(name);
            }
            int count =baseDao.countObject(JDBCUtil.getConn(), countSql , list );
            //3  拼接上 limit 关键字
            sql += " limit ?,? ";// limit 加分页查询
            list.add((pageIndex-1)*pageSize);
            list.add(pageSize);
            //4  执行sql语句 getObjectsRMap(获取数据库链接, sql语句 , sql参数, 替换问号);
            List<Map<String,Object>> l = baseDao.getObjectsRMap(JDBCUtil.getConn(), sql , list);
            //int count =baseDao.countObject(JDBCUtil.getConn(), countSql , list );
            //5 组装返回前端数据
            map.put("data", l);
            map.put("count",count);
            map.put("code", "0000");
            map.put("msg", "查询成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;//返回数据
    }
    //单条数据的查询
    public Map<String,Object> getDoods(int id){
        Map<String,Object> map =new HashMap<String,Object>();
        try {
           //1 编写sql语句
            String sql=" select * from doods where id=? ";
            //2 执行sql语句
            Object doods = (Doods) baseDao.getObject(JDBCUtil.getConn(), sql , Doods.class, new Object[] {id});

            map.put("data",doods);
            map.put("code", "0000");
            map.put("msg", "查询成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;//返回数据
    }
    //数据添加
    public Map<String,Object> addDoods(Doods doods){
        Map<String,Object> map =new HashMap<String,Object>();
        try {
            //1 编写sql语句
            String sql = "insert into Doods values (null, ?, ?, ?, ?, ?, ?)";
            //2 执行sql语句
            baseDao.addObject(JDBCUtil.getConn(), sql,
                    new Object[] {
                            doods.getName(),
                            doods.getType(),
                            doods.getPrice(),
                            doods.getQuantity(),
                            doods.getStatus(),
                            doods.getInfo()
                     });
            map.put("code", "0000");
            map.put("msg", "插入成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;//返回数据
    }
    //数据修改
    public Map<String,Object> updateDoods(Doods doods){
        Map<String,Object> map =new HashMap<String,Object>();
        try {
            //1 编写sql语句
            String sql = "update doods set name=?, type=?, price=?," +
                    "quantity=?, status=?, info=?, where id=?";
            //2 执行sql语句
            baseDao.updateObject(JDBCUtil.getConn(), sql,
                    new Object[] {
                            doods.getName(),
                            doods.getType(),
                            doods.getPrice(),
                            doods.getQuantity(),
                            doods.getStatus(),
                            doods.getInfo(),
                            doods.getId()
                    });
            map.put("code", "0000");
            map.put("msg", "修改成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;//返回数据
    }
    //数据添加
    public Map<String,Object> deleteDoods(int id){
        Map<String,Object> map =new HashMap<String,Object>();
        try {
            //1 编写sql语句
            String sql = "delete from doods where id=?";
            //2 执行sql语句
            baseDao.deleteObject(JDBCUtil.getConn(), sql,
                    new Object[] {id});
            map.put("code", "0000");
            map.put("msg", "删除成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;//返回数据
    }
}
